<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: HCZ <huangchengze@etrol.com> 
// +----------------------------------------------------------------------

namespace app\inspection\controller\api;
use app\common\controller\Api;
use think\response\Json;
use app\inspection\controller\Mission;

/**
 * 巡检报告管理
 * @package app\Report
 * @author HCZ <huangchengze@etrol.com> 
 */
class Report extends Api

{

    /**
   *  巡检报告列表
   * @param string  string operationId巡检员ID,如果有只显示巡检员自己的巡检任务列表.
   * @return Json
   */

	public function reportList($operationId = null){
       $where = [] ;
       if($operationId){
           $where['operationId'] = ['eq',$operationId];
        }
		//分页参数
		if(($limit = input('limit')) && ($page = input('page'))){
			$startLimit =  ($page-1)*$limit;
			// 巡检报告列表
			$missions = collection(model('Mission')->with('user,enterprise,items,items.record,items.record.problem')->where($where)->limit($startLimit.','.$limit)->select())->toArray();
		}else{
			// 巡检报告列表
			$missions = collection(model('Mission')->with('user,enterprise,items,items.record,items.record.problem')->where($where)->select())->toArray();
		}
		foreach ($missions as &$mission){
			foreach($mission['items'] as &$item){
				
				if(isset($item['record']['problem']['inspectionPerson'])){
					$item['record']['problem']['inspectionPersonName'] = get_username($item['record']['problem']['inspectionPerson']);
				}
				if(isset($item['record']['problem']['operationId'])){
					$item['record']['problem']['operationName'] = get_username($item['record']['problem']['operationId']);
				}
			}
		}
	 	//总数
   	 	$count = model('Record')->where($where)->count('id'); 
		  
    	if (empty($info) && $count == 0) {
         	return json(['code'=>0,'msg'=>'没有找到巡检报告','count'=>$count,'data'=>$missions]);
       	} 
       	
       	if ($count > 0){
       		return json(['code'=>1,'msg'=>'巡检报告','count'=>$count,'data'=>$missions]);
       	}
        return json(['code'=>0,'msg'=>'数据异常']);
   	 }

     
   /**
   *   查询巡检记录
   * @param keyword巡检报告名称
   * @param charge巡检员ID
   * @param status状态ID
   * @return Json
   */
   	public function search($issueLevel = null,$status = null ,$start = null,$end = null){
		//分页参数
		$where = [];
		if(($limit = input('limit')) && ($page = input('page'))){
			$startLimit =  ($page-1)*$limit;
			// 巡检报告列表
			$missions = collection(model('Mission')->with('user,enterprise,items,items.record,items.record.problem')->where($where)->limit($startLimit.','.$limit)->select())->toArray();
		}else{
			// 巡检报告列表
			$missions = collection(model('Mission')->with('user,enterprise,items,items.record,items.record.problem')->where($where)->select())->toArray();
		}
		foreach ($missions as &$mission){
			foreach($mission['items'] as $key => &$item){
				if(isset($item['record']['problem']['inspectionPerson'])){
					$item['record']['problem']['inspectionPersonName'] = get_username($item['record']['problem']['inspectionPerson']);
				}
				if(isset($item['record']['problem']['operationId'])){
					$item['record']['problem']['operationName'] = get_username($item['record']['problem']['operationId']);
				}
				if($issueLevel||$status !== ""||$start||$end){
					if($issueLevel && $item['record']['problem']['issueLevel'] != $issueLevel){
						unset($mission['items'][$key]);
					}
					if($status !== "" && $item['record']['problem']['status'] != $status){
						unset($mission['items'][$key]);
					}
					if($start !== ""){
						if(!isset($item['record']['problem']['createTime'])|| strtotime($start) >  strtotime($item['record']['problem']['createTime'])){
							unset($mission['items'][$key]);
						}
					}
					if($end !== ""){
						if(!isset($item['record']['problem']['createTime'])|| strtotime($end) <  strtotime($item['record']['problem']['createTime'])){
							unset($mission['items'][$key]);
						}
					}
				}
			}
			//重排items,使转换JSON时为数组
			$itemList = $mission['items'];
			$mission['items'] = [];
			foreach ($itemList as $value){
				$mission['items'][] = $value;
			}
			
		}
	 	//总数
   	 	$count = model('Record')->where($where)->count('id'); 
		  
    	if (empty($info) && $count == 0) {
         	return json(['code'=>0,'msg'=>'没有找到巡检报告','count'=>$count,'data'=>$missions]);
       	} 
       	
       	if ($count > 0){
       		return json(['code'=>1,'msg'=>'巡检报告','count'=>$count,'data'=>$missions]);
       	}
        return json(['code'=>0,'msg'=>'数据异常']);
      }
      
      /**
       * 添加巡检任务报告
       * @param  array $data
       * @return Json
       */
      public function add($data = NULL){
      	if($data == null){
      		$data = input('post.');
      	}
      	if (!$data['inspectionItemId']) {
      		return json(['code'=>0,'msg'=>'项目ID不存在.请检查']);
      	}
      
      
      	if (empty($data)) {
      		return json(['code'=>0,'msg'=>'数据异常,请检查']);
      	}
      	$model = model('Problem');
      	$result = $model->change($data);
      	if($result !== false){
           setlog('Problemadd','Problem',session('user_auth.id'));
      		return json(['code'=>1,'msg'=>'巡检报告添加成功']);
      	}
      	return json(['code'=>0,'msg'=>'巡检报告添加失败,请检查']);
    }
    
    /**
     * 编辑巡检任务报告
     * @param array $data
     * @return Json
     */
    public function edit($data = null){
    	if($data == null){
    		$data = input('post.');
    	}
    	if (!$data['inspectionItemId']) {
    		return json(['code'=>0,'msg'=>'项目ID不存在.请检查']);
    	}
    	
    	
    	if (empty($data)) {
    		return json(['code'=>0,'msg'=>'数据异常,请检查']);
    	}
    	if($data['status'] == 3){
    		$data['operationTime'] = time();
    	}
    	$model = model('Problem');
    	$result = $model->change($data);
    	if($result !== false){
          setlog('Problemupdate','Problem',session('user_auth.id'));

    		return json(['code'=>1,'msg'=>'巡检报告添加成功']);
    	}
    	return json(['code'=>0,'msg'=>'巡检报告添加失败,请检查']);
    }

}